package com.dhy.utils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ClassUtil {

//    public static void main(String[] args) {
////        fileDfs("com/dhy/controller");
//        String path = "com.dhy.controller";
//        String pathRe = path.replace(".","/");
//        System.out.println("src/main/java/"+pathRe);
////        File file = new File("src\\main\\java\\com\\dhy\\controller");
//        File file = new File("src/main/java/com/dhy/controller");
////        File file1 = new File("J:\\java_project\\promession-demo\\src\\main\\java\\com\\dhy\\controller");
//        System.out.println(file.exists());
//        fileDfs("com.dhy.controller");
//
//        System.out.println("=========================================");
//
//        try {
//            String a = "com/dhy/controller/EmployeeController";
//            String b = a.replace("/",".");
//            Class<?> tClass = Class.forName(b);
//            System.out.println(tClass);
//            for (Method method : tClass.getMethods()) {
//                System.out.println(method.getName());
//            }
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//            System.out.println("异常出现");
//        }
//    }

    public static List<Class<?>> fileDfs(String path) {

        String pathRe = path.replace(".", "/");
        String pathTrue = "src/main/java/" + pathRe;
        System.out.println(pathTrue);

        //类加载器方式获取controller
//        URL url = ClassLoader.getSystemResource("");
//        File[] files = new File(url.getPath() + pathRe).listFiles(file -> file.getName().endsWith(".class"));
//        System.out.println(url);


        List<Class<?>> result = new ArrayList<>();

        File file = new File(pathTrue);
        String className = "";
        if (file.exists() && file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File file1 : files) {
                    if (file1.isDirectory() || file1.getName().substring(0, file1.getName().lastIndexOf(".")).equals("BaseController")) {
                        System.out.println("文件夹不显示");
                    } else {
                        String fileName = file1.getName().substring(0, file1.getName().lastIndexOf("."));
                        className = "com.dhy.controller." + fileName;
                        try {
                            Class<?> tClass = Class.forName(className);
                            result.add(tClass);
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException(e);
                        }

//                        String path1 = file1.getPath().replace("\\",".");
//                        System.out.println(path1);
                    }
                }
            } else {
                System.out.println("文件目录不存在");
            }
        }

        return result;
    }
}
